﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecreate.Business;
using Ecreate.Model;

namespace Ecreate.Web.systemanage.login
{
    public partial class login : System.Web.UI.Page
    {
        private Ecreate.Popedom.Service.IUserService userService = new Ecreate.Popedom.Service.SpringImpl.UserServiceImpl();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_login_Click(object sender, EventArgs e)
        {
            string checkcode=this.txt_checkcode.Text;
            bool ret=Ecreate.Web.utility.verifycode.VerifyCode.CheckInput(this, checkcode);
            if (!ret)
            {
                this.txt_checkcode.Text = "";
                this.txt_password.Text = "";
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('验证码错误');</script>");
                return;
            }
            else
            {
                if (this.txt_username.Text == "")
                {
                    string message = "<script>alert('请输入用户名!');</script>";
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", message);

                }
                if (this.txt_password.Text == "")
                {
                    string message = "<script>alert('请输入密码!');</script>";
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", message);

                }
                if (this.txt_checkcode.Text == "")
                {
                    string message = "<script>alert('请输入验证码!');</script>";
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", message);

                }

                Ecreate.Web.utility.verifycode.VerifyCode.RemoveSession(this);

                Ecreate.Popedom.tSysuser user = new Ecreate.Popedom.tSysuser();
                user.Id = this.txt_username.Text;
                user.Password = this.txt_password.Text;
                SystemUserService service = new SystemUserService();
                Ecreate.Popedom.Web.WebResult<Ecreate.Popedom.tSysuser> result = userService.CheckUser(user);
                if (result.Success == false)
                {
                    this.txt_checkcode.Text = "";
                    this.txt_password.Text = "";
                    string message = "<script>alert('{0}');</script>";
                    message = string.Format(message, result.Message);
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", message);
                }
                else
                {
                    utility.SessionManage.AddSessionObject(this, (Ecreate.Popedom.tSysuser)result.ResultObject);
                    Response.Redirect("/popedom/mainpage.aspx");
                }
            }
        }
    }
}
